<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Controller;
use App\Http\Requests\Api\ActivityEditRequest;
use App\Http\Requests\Api\ActivityRequest;
use App\Models\Activity;
use App\Models\ActivityProcess;
use App\Models\Admin;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ActivityController extends Controller
{




    //活动列表
    public function getActivityList(Request $request){

        $where = function($query) use ($request){
            if($request->title){//活动名称
                $query->where('title','like', '%'.$request->title.'%');
            }
            if($request->start_time){//活动时间 开始
                $query->where('start_time','>',$request->start_time);
            }
            if($request->end_time){//活动时间 结束
                $query->where('end_time','<',$request->end_time);
            }
            if($request->join_start_time&&$request->join_end_time){ //活动报名时间
                $query->whereBetween('join_time',[$request->join_start_time,$request->join_end_time]);
            }

            if($request->status){//活动状态 举办中1 准备中2  已结束3 4报名中
                $time = date("Y-m-d H:i:s");
                if($request->status==1){
                    $query->where('start_time','<',$time)->where('end_time','>',$time);
                }
                if($request->status==2){
                    $query->where('start_time','>',$time)->where('join_time','<',$time);
                }
                if($request->status==3){
                    $query->where('end_time','<',$time);
                }
                if($request->status==4){
                    $query->where('join_time','>',$time);
                }

            }

            if($request->check_rule||$request->check_rule===0||$request->check_rule==='0'){// 1报名后需要审核
                    $query->where('check_rule',$request->check_rule);

            }

            if($request->checked){// 代表核销端过来的
                $admin = Auth::guard('admin')->user();

                $query->whereRaw("FIND_IN_SET('".$admin['id']."',admin_id)");

            }
        };

        $list = Activity::with('process:id,activity_id,sort,content')
            ->with(['order' => function ($query) {
                $query->where('pay_status',1)->where('order_status','<',4);
            },])
            ->where($where)->orderBy('id','desc')
            //->select(['id','title','price','person','province','city','district','address','admin_id','is_del','pay_type','start_time','end_time','join_time','check_rule','cancel_rule','cover_img','mobile'])
            ->paginate($request->pageSize?:15);
        foreach ($list as $k=>$v){
            $list[$k]['order_user_count'] = 0;
            if($v['order']){
                foreach ($v['order'] as $kk=>$vv){
                    $list[$k]['order_user_count']+=$vv['person'];
                }
            }

            unset($list[$k]['order']);
        }

        return $this->success($list);
    }


    //活动详情
    public function getActivityDetail(Request $request){

        $id = $request->id;

        if(!$id){
           return $this->failed("id必传");
        }else{
           $info = Activity::with('process')->find($id);

           return $this->success($info);
        }

    }

    //编辑活动
    public function editActivity(ActivityEditRequest $request){

        $activity = Activity::find($request->id);
        $activity->fill($request->all()); //将值赋值到模型

        if($activity->update()){

            $data = [];

            foreach ($request->process as $k=>$v){
                $data[$k]['activity_id'] = $activity->id;
                $data[$k]['sort'] = $v['sort'];
                $data[$k]['content'] = $v['content'];
                $data[$k]['created_at'] = date("Y-m-d H:i:s");
                $data[$k]['updated_at'] =date("Y-m-d H:i:s");
            }
            ActivityProcess::where('activity_id',$request->id)->delete();
            if(ActivityProcess::insert($data))
                return $this->success('编辑成功');

        }else{
            return $this->failed('编辑失败');
        }

    }


    //添加活动记录
    public function addActivity(ActivityRequest $request){




        if($request->join_time > $request->start_time)

            return $this->failed('报名截止时间不能大于活动开始时间');

        if($request->start_time >= $request->end_time)

            return $this->failed('活动开始时间不能大于截止时间');

        $activity = Activity::create($request->all());

        if(!$activity){

            return $this->failed('添加失败');
        }

        $data = [];

        foreach ($request->process as $k=>$v){
            $data[$k]['activity_id'] = $activity->id;
            $data[$k]['sort'] = $v['sort'];
            $data[$k]['content'] = $v['content'];
            $data[$k]['created_at'] = date("Y-m-d H:i:s");
            $data[$k]['updated_at'] =date("Y-m-d H:i:s");
        }

        if(ActivityProcess::insert($data))
            return $this->success('添加成功');

        return $this->failed('添加失败');

    }



    public function ActivityIsDel(Request $request){
        $activity_id = $request->activity_id;
        $is_del = $request->is_del;//  0禁用   1启用
        if(!$activity_id)
            return $this->failed("活动id必传");
        $order = Order::where('activity_id',$activity_id)->where('pay_status',1)->first();

        $activity = Activity::find($activity_id);

        if($activity->end_time > date("Y-m-d H:i:s")){
            if($order)
                return $this->failed("存在关联订单");
        }


        $res = Activity::where('id',$activity_id)->update(['is_del'=>$is_del]);


        if($res)
            return $this->success("操作成功");

        return $this->failed("操作失败");

    }

    public function getAdminList(Request $request){

        $where = function($query) use ($request){
            if($request->name){//姓名
                $query->where('name','like', '%'.$request->name.'%');
            }
            if($request->mobile){//电话
                $query->where('mobile','like', '%'.$request->mobile.'%');
            }

        };

        $list = Admin::where('role_id','>',0)->where($where)->select(['id','name','mobile'])->get();

        return $this->success($list);

    }

}
